<template>
  <d2-container>
    <el-alert
      title="由于功能特殊，请注意在需要时刷新您的浏览器（以重置路由设置）查看效果"
      type="warning"
      show-icon/>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOneClick()">新增</el-button>
        <el-button type="danger" @click="maybeDelete()" :disabled="selectListNum <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <my-table
      v-on:dataListSelections="maybeDelete"
      v-bind:listLoading="listLoading"
      :dataList="dataList"
    >

    </my-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-one v-if="addVisible" ref="addOne" @refreshDataList="getDataList"></add-one>
  </d2-container>
</template>

<script>
import AddOne from './userrouter-add-or-update'
import MyTable from './userrouter-table'
import { reqList } from '@api/userrouter.api.fetch'

export default {
  name: 'system-router',
  data () {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      listLoading: false,
      addVisible: false,
      selectListNum: 0
    }
  },
  components: {
    AddOne,
    MyTable
  },
  mounted () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      reqList({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'key': this.dataForm.key
      })
        .then(res => {
          this.dataList = res.data
          this.totalPage = res.data.totalCount
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOneClick (id) {
      this.addVisible = true
      this.$nextTick(() => {
        this.$refs.addOne.init(id)
      })
    },
    maybeDelete (selectNum) {
      this.selectListNum = selectNum
    }
  }
}
</script>
